package com.carpedil.auth.controller;

import com.carpedil.auth.entity.UserEntity;
import com.carpedil.auth.service.UserService;
import com.carpedil.common.dto.UserDTO;
import com.carpedil.util.JwtUtil;
import com.carpedil.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/** @author Administrator */
@Controller
@RequestMapping("/auth")
public class AuthController {
  @Autowired UserService userService;

  @GetMapping("user/{username}/{password}")
  @ResponseBody
  public String authorize(
      @PathVariable("username") String username, @PathVariable("password") String password) {
    if (null != username && null != password) {
      UserEntity user = userService.findUser(username, password);
      UserDTO dto =
          new UserDTO()
              .setUsername(user.getUsername())
              .setPassword(user.getPassword())
              .setPerm(user.getPerm());

      return JwtUtil.createToken(dto);
    }
    return StringUtil.INVALID_USER;
  }

  @GetMapping("/toLogin")
  public String toLogin() {
    return "login";
  }
}
